# 打印菱形
def print_diamond(width):
    # 上半部分
    for i in range(width // 2 + 1):
        print(' ' * (width // 2 - i) + '*' * (2*i + 1))
    # 下半部分
    for i in range(width // 2 - 1, -1, -1):
        print(' ' * (width // 2 - i) + '*' * (2*i + 1))

# 调用函数打印宽度为 7 的菱形
print_diamond(7)
# 打印水仙花数
def print_narcissistic_numbers():
    for num in range(100, 1000):  # 遍历所有的三位数
        # 分离每一位数字
        hundreds = num // 100
        tens = (num // 10) % 10
        ones = num % 10

        # 计算每位数字的三次方之和
        sum_of_powers = hundreds ** 3 + tens ** 3 + ones ** 3

        # 判断是否为水仙花数
        if sum_of_powers == num:
            print(num)


# 执行函数
print_narcissistic_numbers()

# 打印九九乘法表
def print_multiplication_table():
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f"{j}x{i}={i * j}", end="\t")
        print()  # 换行

# 执行函数
print_multiplication_table()

# 打印完数
def is_perfect_number(n):
    """判断一个数是否为完美数"""
    if n < 1:
        return False

    divisors_sum = 0
    for i in range(1, n):
        if n % i == 0:
            divisors_sum += i

    return divisors_sum == n


def print_perfect_numbers(start, end):
    """打印指定范围内的所有完美数"""
    perfect_numbers = []
    for num in range(start, end + 1):
        if is_perfect_number(num):
            perfect_numbers.append(num)

    return perfect_numbers


# 打印 1 到 10000 范围内的所有完美数
perfect_numbers = print_perfect_numbers(1, 10000)
print(perfect_numbers)

# 打印质数
def is_prime(n):
    """判断一个数是否为质数"""
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

def print_primes(start, end):
    """打印指定范围内的所有质数"""
    primes = []
    for num in range(start, end + 1):
        if is_prime(num):
            primes.append(num)
    return primes

# 打印 1 到 100 范围内的所有质数
primes = print_primes(1, 100)
print(primes)

# 打印斐波那契数列
def fibonacci_dp(n):
    """使用动态规划生成斐波那契数列的前 n 个数"""
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]

    fib = [0] * n
    fib[0] = 0
    fib[1] = 1
    for i in range(2, n):
        fib[i] = fib[i - 1] + fib[i - 2]
    return fib

# 打印前 10 个斐波那契数
fibonacci_sequence = fibonacci_dp(10)
print(fibonacci_sequence)
